﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FeedReaderApp.Model;
using FeedReaderApp.ViewModel;
using GalaSoft.MvvmLight;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace FeedReaderApp
{
    public sealed partial class ItemDetailPage : FeedReaderApp.Common.LayoutAwarePage
    {
        public ItemDetailViewModel ViewModel { get; set; }

        public ItemDetailPage()
        {
            this.InitializeComponent();

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                ViewModel = new ItemDetailViewModel();
                DataContext = ViewModel;
            }
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            DataTransferManager.GetForCurrentView().DataRequested += OnDataRequested;

            if (pageState != null && pageState.ContainsKey(Constants.ItemDetailPageState))
            {
                navigationParameter = pageState[Constants.ItemDetailPageState];
            }

            ViewModel.InitializeFromFeedItemUniqueId((string)navigationParameter);
            var left = ViewModel.Items.IndexOf(ViewModel.SelectedItem);

            ButtonLeft.Visibility = left > 0 ? Visibility.Visible : Visibility.Collapsed;
            ButtonRight.Visibility = left < ViewModel.Items.Count - 1 ? Visibility.Visible : Visibility.Collapsed;
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            try
            {
                DataTransferManager.GetForCurrentView().DataRequested -= OnDataRequested;
                var selectedItem = (FeedItem)this.ViewModel.SelectedItem;
                pageState[Constants.ItemDetailPageState] = selectedItem.UniqueId;
            }
            catch
            {
            }
        }

        private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            try
            {
                ViewModel.Share(e);
            }
            catch
            {
            }
        }

        private void ButtonClickLeft(object sender, RoutedEventArgs e)
        {
            var left = ViewModel.Items.IndexOf(ViewModel.SelectedItem) - 1;
            if (left < 0) return;

            ViewModel.SelectedItem = ViewModel.Items[left];
            ButtonLeft.Visibility = left > 0 ? Visibility.Visible : Visibility.Collapsed;
            ButtonRight.Visibility = left < ViewModel.Items.Count - 1 ? Visibility.Visible : Visibility.Collapsed;
        }

        private void ButtonClickRight(object sender, RoutedEventArgs e)
        {
            var right = ViewModel.Items.IndexOf(ViewModel.SelectedItem) + 1;
            if (right > ViewModel.Items.Count - 1) return;

            ViewModel.SelectedItem = ViewModel.Items[right];
            ButtonRight.Visibility = right < ViewModel.Items.Count - 1 ? Visibility.Visible : Visibility.Collapsed;
            ButtonLeft.Visibility = right > 0 ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
